{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "31adac63-ca7c-4ead-a5ea-0f747e8bfa21",
   "metadata": {},
   "source": [
    "使用LangSmish追踪应用程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "57057bcc-be28-4265-8ab3-0f17d0ac3654",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
    "os.environ[\"LANGCHAIN_API_KEY\"] = \"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\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25885288-317b-4ba3-abc9-8183facb62ef",
   "metadata": {},
   "source": [
    "调用语言模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b89f80f3-0a49-491e-9e88-21eddbf07947",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "model = ChatOpenAI(model=\"gpt-3.5-turbo\", temperature=0, api_key='sk-zk296e1a1b3cdb88c1be349e0da6283e0036757dc77b0f0d', base_url='https://api.zhizengzeng.com/v1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2c1bde78-67dd-4703-9d0c-c28c352d1a0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.messages import HumanMessage, SystemMessage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ee61418b-8f4f-4bce-8c2f-3c34e49cfede",
   "metadata": {},
   "outputs": [],
   "source": [
    "messages = [\n",
    "    SystemMessage(content=\"Translate the following from English into Chinese\"),\n",
    "    HumanMessage(content=\"good!\"),\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ea33f25a-c962-46e4-8c6f-2871d16fe741",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='好的！', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 3, 'prompt_tokens': 20, 'total_tokens': 23, 'completion_tokens_details': {'accepted_prediction_tokens': None, 'audio_tokens': None, 'reasoning_tokens': None, 'rejected_prediction_tokens': None}, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': None, 'cached_tokens_details': []}}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-52d02887-e890-4a98-a09d-87f0ae395d47-0', usage_metadata={'input_tokens': 20, 'output_tokens': 3, 'total_tokens': 23, 'input_token_details': {}, 'output_token_details': {}})"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.invoke(messages)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ebf6545-562a-474d-b449-915e9f3b973b",
   "metadata": {},
   "source": [
    "输出解析器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "02b81146-8a85-44c5-98be-8145ba19d886",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.output_parsers import StrOutputParser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "56dc6a6e-2229-42cb-ac8c-abd0ace9a5d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "parser = StrOutputParser()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7d26b405-836f-4e1e-af2f-fd1a29100355",
   "metadata": {},
   "outputs": [],
   "source": [
    "chain = model | parser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "85d7c470-24a9-4053-bc08-c289b836bfa8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'好的！'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain.invoke(messages)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2792f0ba-200b-41da-a737-0793790cd938",
   "metadata": {},
   "source": [
    "提示词"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "4f5e0b7d-783a-4de7-8e4a-90a86e00b5ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "281973d9-b3c1-4511-8351-4082019faf51",
   "metadata": {},
   "outputs": [],
   "source": [
    "system_template = \"Translate the following into {language}:\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "800c871a-b934-46bc-986a-6b45ca80f188",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt_template = ChatPromptTemplate.from_messages(\n",
    "    [(\"system\", system_template), (\"user\", \"{text}\")]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "01fef50e-3663-47e4-a8c5-f359a2bac4f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 动态生成提示\n",
    "prompt = prompt_template.format(language=\"French\", text=\"Hello, how are you?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "a4a798d0-d227-47f4-9ee8-f8f623dc4a09",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ChatPromptValue(messages=[SystemMessage(content='Translate the following into English:', additional_kwargs={}, response_metadata={}), HumanMessage(content='是的', additional_kwargs={}, response_metadata={})])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = prompt_template.invoke({\"language\": \"English\", \"text\": \"是的\"})\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "8eb0f972-27dc-497f-a94f-e8afe1f4104f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[SystemMessage(content='Translate the following into English:', additional_kwargs={}, response_metadata={}),\n",
       " HumanMessage(content='是的', additional_kwargs={}, response_metadata={})]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.to_messages()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49c4bcb7-e968-4355-b576-0db9d5fce216",
   "metadata": {},
   "source": [
    "使用 LCEL 连接组件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "25db3f34-e838-455e-800c-3f7349ed100a",
   "metadata": {},
   "outputs": [],
   "source": [
    "chain = prompt_template | model | parser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "88a61eed-ae65-438b-8a60-c08624076a58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Yes'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain.invoke({\"language\": \"English\", \"text\": \"是的\"})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
